#include "dlgsettings.h"
#include "ui_dlgsettings.h"
#include <QFileDialog>

DlgSettings::DlgSettings(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DlgSettings)
{
    ui->setupUi(this);
    mp_settings = new QSettings("SKRware", "Lak-Housing-DBMS",this);
    read_settings();
    connect(ui->btn_lender_add, SIGNAL(clicked()), this, SLOT(add_lender_item()));
    connect(ui->btn_lender_del, SIGNAL(clicked()), this, SLOT(del_lender_item()));
    QTableWidgetItem *p_it;
    p_it = new QTableWidgetItem(RUSTR("Должность предствителя наймодателя"));
    ui->tw_lenders->setHorizontalHeaderItem(0, p_it);
    p_it = new QTableWidgetItem(RUSTR("И.О. Фамилия"));
    ui->tw_lenders->setHorizontalHeaderItem(1, p_it);
}

DlgSettings::~DlgSettings()
{
    delete ui;
}

void DlgSettings::on_btn_browse_dir_clicked()
{
    QFileDialog dlg_open_dir(NULL, RUSTR("Выбор каталога"));
    dlg_open_dir.setFileMode(QFileDialog::Directory);
    if(!dlg_open_dir.exec()) return;
    ui->ledit_home_path->setText(dlg_open_dir.selectedFiles().at(0));
}

void DlgSettings::write_settings()
{
    mp_settings->setValue(HOME_DIR_KEY, QVariant(ui->ledit_home_path->text()));
    mp_settings->setValue(AUTO_CONNECT_KEY, QVariant(ui->chbox_auto_connect->isChecked()));
    mp_settings->setValue(DOC_DIR_KEY, QVariant(ui->ledit_doc_path->text()));
    mp_settings->setValue(EXPORT_DIR_PATH, QVariant(ui->ledit_export_dir->text()));
    QStringList qstrl_lender;
    QStringList qstrl_lenders_fio;
    for(int i = 0; i<ui->tw_lenders->rowCount(); i++)
    {
        qstrl_lender.append(ui->tw_lenders->item(i,0)->text());
        qstrl_lenders_fio.append(ui->tw_lenders->item(i,1)->text());
    }
    mp_settings->setValue(LENDERS_LIST, qstrl_lender);
    mp_settings->setValue(LENDERS_FIO_LIST, qstrl_lenders_fio);

}

void DlgSettings::read_settings()
{
    ui->ledit_home_path->setText(mp_settings->value(HOME_DIR_KEY).toString());
    ui->chbox_auto_connect->setChecked(mp_settings->value(AUTO_CONNECT_KEY).toBool());
    ui->ledit_doc_path->setText(mp_settings->value(DOC_DIR_KEY).toString());
    ui->ledit_export_dir->setText(mp_settings->value(EXPORT_DIR_PATH).toString());
    ui->tw_lenders->setRowCount(0);
    QStringList qstrl_lender = mp_settings->value(LENDERS_LIST).toStringList();
    QStringList qstrl_lenders_fio = mp_settings->value(LENDERS_FIO_LIST).toStringList();
    for(int i = 0; i<qstrl_lender.size(); i++)
        add_lender_item(qstrl_lender.at(i), qstrl_lenders_fio.at(i));

}

void DlgSettings::get_housing_settings(HousingSettings &hs)
{
    hs.str_home_path = ui->ledit_home_path->text();
}

bool DlgSettings::is_auto_connect()
{
    return ui->chbox_auto_connect->isChecked();
}

void DlgSettings::on_btnOk_clicked()
{
    write_settings();
    lakhMsg()<<RUSTR("Настройки сохранены.");
}

void DlgSettings::on_btnCancel_clicked()
{
    read_settings();
    lakhMsg()<<RUSTR("Настройки загружены.");
}

void DlgSettings::on_btn_browse_dir_doc_clicked()
{
    QFileDialog dlg_open_dir(NULL, RUSTR("Выбор каталога"));
    dlg_open_dir.setFileMode(QFileDialog::Directory);
    if(!dlg_open_dir.exec()) return;
    ui->ledit_doc_path->setText(dlg_open_dir.selectedFiles().at(0));
}

QString DlgSettings::get_doc_dirpath()
{
    return ui->ledit_doc_path->text();
}

void DlgSettings::on_btn_set_export_dir_clicked()
{
    QFileDialog dlg_open_dir(NULL, RUSTR("Выбор каталога"));
    dlg_open_dir.setFileMode(QFileDialog::Directory);
    if(!dlg_open_dir.exec()) return;
    ui->ledit_export_dir->setText(dlg_open_dir.selectedFiles().at(0));
}

QString DlgSettings::get_export_dirpath()
{
    return ui->ledit_export_dir->text();
}

void DlgSettings::add_lender_item(QString qstr_lender, QString qstr_lender_fio)
{
    int r = ui->tw_lenders->rowCount();
    ui->tw_lenders->setRowCount(r+1);
    QTableWidgetItem *p_it;
    p_it = new QTableWidgetItem(qstr_lender);
    p_it->setFlags(Qt::ItemIsEnabled|Qt::ItemIsSelectable|Qt::ItemIsEditable);
    ui->tw_lenders->setItem(r, 0, p_it);
    p_it = new QTableWidgetItem(qstr_lender_fio);
    p_it->setFlags(Qt::ItemIsEnabled|Qt::ItemIsSelectable|Qt::ItemIsEditable);
    ui->tw_lenders->setItem(r, 1, p_it);
}

void DlgSettings::del_lender_item()
{
    int r = ui->tw_lenders->currentRow();
    if (r < 0) return;
    ui->tw_lenders->removeRow(r);
}

void DlgSettings::showEvent(QShowEvent *)
{
    resize_tables();
}


void DlgSettings::resizeEvent(QResizeEvent *p_e)
{
    resize_tables();
    QDialog::resizeEvent(p_e);
}

void DlgSettings::resize_tables()
{
    int w = ui->tw_lenders->width();
    ui->tw_lenders->setColumnWidth(0,w*65/100);
    ui->tw_lenders->setColumnWidth(1,w*30/100);
}

QStringList DlgSettings::get_lender_heads()
{
    QStringList qstrl_lender;
    for(int i = 0; i<ui->tw_lenders->rowCount(); i++)
    {
        qstrl_lender.append(ui->tw_lenders->item(i,0)->text());
    }
    return qstrl_lender;
}

QStringList DlgSettings::get_lender_heads_fio()
{
    QStringList qstrl_lenders_fio;
    for(int i = 0; i<ui->tw_lenders->rowCount(); i++)
    {
        qstrl_lenders_fio.append(ui->tw_lenders->item(i,1)->text());
    }

    return qstrl_lenders_fio;
}
